/*
 * Copyright @ 苏州瑞云信息技术有限公司 All rights reserved.
 */
package cn.com.recloud.bbs.service.impl;

import cn.com.recloud.bbs.constant.MetaOptionConstant;
import cn.com.recloud.bbs.dto.CampaignAnalysisDTO;
import cn.com.recloud.bbs.entity.bbs_shared_log;
import cn.com.recloud.bbs.dao.SharedLogDao;
import cn.com.recloud.bbs.entity.metaoption;
import cn.com.recloud.bbs.service.MetaoptionService;
import cn.com.recloud.bbs.service.SharedLogService;
import cn.com.recloud.bbs.util.UserUtil;
import cn.com.recloud.bbs.vo.SharedLogVO;
import cn.com.recloud.core.constant.SysConstant;
import cn.com.recloud.one.spring.service.impl.BaseServiceImpl;
import cn.com.recloud.one.spring.util.ExportUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kim dong
 * @date 2020-06-08
 */
@Service
public class SharedLogServiceImpl extends BaseServiceImpl<SharedLogDao, bbs_shared_log> implements SharedLogService {
    @Resource
    private MetaoptionService metaoptionService;
    @Autowired
    private SharedLogDao sharedLogDao;
    @Autowired
    private UserUtil userUtil;

    /**
     * pc分享明细列表
     * @param dto
     * @return
     */
    @Override
    public Map serchShared(CampaignAnalysisDTO dto) {
        Map map = new HashMap();
        dto.setPageIndex((dto.getPageIndex()-1) * dto.getPageSize());
        List<SharedLogVO> list = sharedLogDao.listSharedLog(dto);
        Integer count = sharedLogDao.listCountSharedLog(dto);
        map.put("list",list);
        map.put("count",count);
        return map;
    }

    /**
     * 导出分享明细
     * @param campaign
     * @param response
     */
    @Override
    public void exportShared(CampaignAnalysisDTO campaign, HttpServletResponse response) {
        List<SharedLogVO> sharedLogs =sharedLogDao.exportSharedLog(campaign);
        ExportUtil<SharedLogVO> exportUtil = new ExportUtil<>();
        exportUtil.export(response,sharedLogs,campaign.getExportMap(),campaign.getExportName());
    }

    @Override
    public boolean saveShareLog(bbs_shared_log bbsSharedLog,String type) {
        if("campaign".equals(type)){
            bbsSharedLog.setObject_type_name(MetaOptionConstant.CAMPAIGN_ENTITY_NAME);
        }else{
            bbsSharedLog.setObject_type_name(MetaOptionConstant.ARTICAL_ENTITY_NAME);
        }
        //记录分享途径
        metaoption metaoption1 = metaoptionService.findMetaOptionBySetNameAndValue(MetaOptionConstant.SHARE_SCENE,bbsSharedLog.getShare_scene());
        bbsSharedLog.setShare_scene(metaoption1.getValue());
        bbsSharedLog.setShare_scenename(metaoption1.getLabel());
        String userName = userUtil.getUserName();
        String userId = userUtil.getUserId();
        metaoption metaoption = null;
        //未获取到用户信息则用户未登录
        //未登录就是联系人，登录则是系统用户
        if(SysConstant.DEFAULT_ID.equals(userId)){
            metaoption = metaoptionService.findMetaOptionBySetNameAndValue(MetaOptionConstant.SIGNUP_TYPE,"1");
        }else{ //用户登录
            metaoption = metaoptionService.findMetaOptionBySetNameAndValue(MetaOptionConstant.SIGNUP_TYPE,"2");
        }
        bbsSharedLog.setShared_by_type(metaoption.getValue());
        bbsSharedLog.setShared_by_id(userId);
        bbsSharedLog.setShared_by_name(userName);
        bbsSharedLog.setName(bbsSharedLog.getObject_name());
        return super.save(bbsSharedLog);
    }
}
